//
//  BillCardViewTest.swift
//  Speek
//
//  Created by MikeHo on 2023/6/25.
//

import SwiftUI

struct BillCardViewTest: View {
    
    @State private var date = Date()
    @State private var isInOrOut = false
    @State private var isShowEdit = false
    
    var body: some View {
       
            HStack(spacing:10){
                Image("旅行")
                    .resizable()
                    .frame(width: 60, height: 60)
                
                VStack{
                    Text("旅行")
                        .font(.subheadline)
                    Text(dateFormatter(date:date))
                        .font(.subheadline)
                }
                .padding(.horizontal,5)
                .frame(width: 60, height: 60)
                .foregroundColor(.white)
                
                
                Text("买一个超级大的的的大蛋糕")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(width: 120)
                
                Text(getInOrOut(tag:isInOrOut)+"1000.22")
                    .frame(width: 100, height: 60)
                    .foregroundColor(isInOrOut ? Color(red:0,green: 0.725,blue: 0.678):Color(red:100,green: 0.3412,blue: 0.2))
                    .background(RoundedRectangle(cornerRadius: 10)
                        .stroke(isInOrOut ? Color(red:0,green: 0.725,blue: 0.678).opacity(0.5):Color(red:100,green: 0.3412,blue: 0.2).opacity(0.5), lineWidth: 2))
                
                if isShowEdit{
                    VStack(spacing: 10){
                        //编辑账单按钮
                        Button(action: {
                            
                        }) {
                            Image(systemName: "square.and.pencil")
                            //.frame(width: 20, height: 20)
                                .aspectRatio(contentMode: .fit)
                                .foregroundColor(.green)
                        }
                        //删除账单按钮
                        Button(action: {
                        }) {
                            Image(systemName: "trash")
                                .frame(width: 20, height: 20)
                                .foregroundColor(.red)
                        }
                    }
                    .padding(.trailing,30)
                }
            }
            .gesture(swipeToDelete)
            .onTapGesture {
                isShowEdit = false
            }
            .frame(width: 390, height: 100)
            .background(Color(red:0.04,green: 0.09,blue: 0.16).cornerRadius(30))
            .preferredColorScheme(.dark)
            .clipped()
        
    }
                     
    func getInOrOut(tag:Bool)->String{
        if tag {
            return "+¥"
        }else{
            return "-¥"
        }
    }
    
    private func dateFormatter(date:Date)->String{
        let Formatter = DateFormatter()
        Formatter.dateFormat = "MM-dd"
        let formattedDate = Formatter.string(from: date)
        return formattedDate
    }
    
    private var swipeToDelete: some Gesture {
        DragGesture()
            .onEnded { gesture in
                if gesture.translation.width < 100 {
                    isShowEdit = true
                }
            }
    }
}
    
struct BillCardViewTest_Previews: PreviewProvider {
    static var previews: some View {
        
        BillCardViewTest()
            //.background(Color(red:0.04,green: 0.09,blue: 0.16).cornerRadius(20))
            //.previewLayout(.fixed(width: 350, height: 100))
    }
}

